package com.artislong.config;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.nio.charset.Charset;

/**
 *
 * @author 陈敏
 * @version Text2SqlConfiguration.java, v 1.0 2025 07 02 23:15 chenmin Exp $
 * Created on 2025 07 02
 */
@Configuration
public class Text2SqlConfiguration {
    @Bean
    public PromptTemplate systemPrompt(
            @Value("classpath:system-prompt.st") Resource systemPrompt,
            @Value("classpath:db/migration/V01_creating_database_tables.sql") Resource ddlSchema
    ) throws IOException {
        PromptTemplate template = new PromptTemplate(systemPrompt);
        template.add("ddl", ddlSchema.getContentAsString(Charset.defaultCharset()));
        return template;
    }

    @Bean
    public ChatClient chatClient(ChatModel chatModel, PromptTemplate systemPrompt) {
        return ChatClient
                .builder(chatModel)
                .defaultSystem(systemPrompt.render())
                .build();
    }
}
